﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.ViewModels;
using WCYCReader.Main.Core;
using WCYCReader.Models;
using WCYCReader.Main;
using WCYCReader.Utility;
using System.Windows.Media;

namespace WCYCReader.Pages
{
    public partial class BookRecommendPage : PhoneApplicationPage
    {
        protected PerformanceProgressBar BoyProgressBar = new PerformanceProgressBar();

        protected PerformanceProgressBar GirlProgressBar = new PerformanceProgressBar();

        protected GirlBookList_VM gbl_vm = new GirlBookList_VM();

        protected BoyBookList_VM bbl_vm = new BoyBookList_VM();

        protected bool BoyLoading;

        protected bool GirlLoading;

       

        public BookRecommendPage()
        {
            this.InitializeComponent();
        }

        protected void AddPerformanceProgressBar(QQRecommendPageProgressBar pro)
        {
            switch (pro)
            {
                case QQRecommendPageProgressBar.BoyPro:
                    {
                        if (this.BoyProgressBar.IsIndeterminate)
                        {
                            break;
                        }
                        this.BoyProgressBar.IsIndeterminate = true;
                        this.BoyProgressBar.Height = 20;
                        this.BoyProgressBar.Foreground = new SolidColorBrush(Colors.Red);
                        this.RemovePerformanceProgressBar(this.GirlProgressBar);
                        return;
                    }
                case QQRecommendPageProgressBar.GirlPro:
                    {
                        if (this.GirlProgressBar.IsIndeterminate)
                        {
                            break;
                        }
                        this.GirlProgressBar.IsIndeterminate = true;
                        this.GirlProgressBar.Height = 20;
                        this.GirlProgressBar.Foreground = new SolidColorBrush(Colors.Red);
                        this.RemovePerformanceProgressBar(this.BoyProgressBar);
                        return;
                    }
                default:
                    {
                        this.RemovePerformanceProgressBar(this.BoyProgressBar);
                        this.RemovePerformanceProgressBar(this.GirlProgressBar);
                        break;
                    }
            }
        }

        private void BookRecommendPage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.RecommendPagePivot.SelectedIndex  != 0 && this.gbl_vm.T_GirlBookList_VM().Count == 0 && !this.GirlLoading)
            {
                App.QQReader_prePageId = App.QQReader_pageId;
                App.QQReader_pageId = "QQReader_RecommendPage_type3";
                this.AddPerformanceProgressBar(QQRecommendPageProgressBar.GirlPro);
                this.GirlLoading = true;
                Dictionary<string, string> strs = new Dictionary<string, string>()
                {
                    { "type", "3" },
                    { "pageSize", "20" },
                    { "fromWho", "4" }
                };
                HttpPostClient httpPostClient = new HttpPostClient(strs, GetHttpInfo.GetHttpHeaders());
                httpPostClient.DownloadStringAsync(new Uri("http://appassist.3g.qq.com/rank/levelOne", UriKind.Absolute));
                httpPostClient.DownloadStringCompleted += new HttpPostClient.DownloadStringCompletedHandler(this.girlBook_DownloadStringCompleted);
            }
        }

        private void boyBook_DownloadStringCompleted(object sender,WCYCReader.Main.Core.DownloadStringCompletedEventArgs e)
        {
            base.Dispatcher.BeginInvoke(() =>
            {
                if (e.Result != null)
                {
                    JsonData jsonDatum = new JsonData();
                    jsonDatum = ParseJson.Parse<JsonData>(e.Result);
                    this.bbl_vm.SetBoyBookList(jsonDatum);
                }
                else
                {
                    Prompt.PromptMessage("提示：网络异常！");
                }
                this.BoyLoading = false;
                this.RemovePerformanceProgressBar(this.BoyProgressBar);
            });
        }

        private void boyBookListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                SettingHelper.SaveInToSettings("BookDetail", this.bbl_vm.T_BoyBookList_VM()[listBox.SelectedIndex]);
                NaviService.Navigate(QQReaderPage.EBookDetailPage, "");
            }
            listBox.SelectedIndex = -1;
        }

        private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.boyBookListBox.ItemsSource = this.bbl_vm.T_BoyBookList_VM();
            this.girlBookListBox.ItemsSource = this.gbl_vm.T_GirlBookList_VM();
            this.LayoutRoot.Children.Add(this.GirlProgressBar);
            this.LayoutRoot.Children.Add(this.BoyProgressBar);
        }

        private void ContentGrid_Unloaded(object sender, RoutedEventArgs e)
        {
            this.AddPerformanceProgressBar(QQRecommendPageProgressBar.RemoveAll);
            this.LayoutRoot.Children.Remove(this.GirlProgressBar);
            this.LayoutRoot.Children.Remove(this.BoyProgressBar);
        }

        private void girlBook_DownloadStringCompleted(object sender, WCYCReader.Main.Core.DownloadStringCompletedEventArgs e)
        {
            base.Dispatcher.BeginInvoke(() =>
            {
                if (e.Result != null)
                {
                    JsonData jsonDatum = new JsonData();
                    jsonDatum = ParseJson.Parse<JsonData>(e.Result);
                    this.gbl_vm.SetGirlBookList(jsonDatum);
                }
                else
                {
                    Prompt.PromptMessage("提示：网络异常！");
                }
                this.GirlLoading = false;
                this.RemovePerformanceProgressBar(this.GirlProgressBar);
            });
        }

        private void girlBookListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox.SelectedIndex >= 0)
            {
                SettingHelper.SaveInToSettings("BookDetail", this.gbl_vm.T_GirlBookList_VM()[listBox.SelectedIndex]);
                NaviService.Navigate(QQReaderPage.EBookDetailPage, "");
            }
            listBox.SelectedIndex = -1;
        }



        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App.QQReader_prePageId = App.QQReader_pageId;
            App.QQReader_pageId = "QQReader_RecommendPage_type2";
            if (this.bbl_vm.T_BoyBookList_VM().Count == 0 && !this.BoyLoading)
            {
                this.AddPerformanceProgressBar(QQRecommendPageProgressBar.BoyPro);
                this.BoyLoading = true;
                Dictionary<string, string> strs = new Dictionary<string, string>()
                {
                    { "type", "2" },
                    { "pageSize", "20" },
                    { "fromWho", "4" }
                };
                HttpPostClient httpPostClient = new HttpPostClient(strs, GetHttpInfo.GetHttpHeaders());
                httpPostClient.DownloadStringAsync(new Uri("http://appassist.3g.qq.com/rank/levelOne", UriKind.Absolute));
                httpPostClient.DownloadStringCompleted += new HttpPostClient.DownloadStringCompletedHandler(this.boyBook_DownloadStringCompleted);
            }
        }

        protected void RemovePerformanceProgressBar(PerformanceProgressBar pro)
        {
            if (pro.IsIndeterminate)
            {
                pro.IsIndeterminate = false;
            }
        }
    }
}